
//
//  ZBWPhotoManager.m
//  ZBWAlbum
//
//  Created by 张博文 on 2020/3/18.
//  Copyright © 2020 com.bw. All rights reserved.
//


#import "ZBWPhotosManager.h"
#import "ZBWAlbumViewController.h"
#import "ZBWPhotoManager.h"

@implementation ZBWPhotosManager

+ (ZBWPhotoManager*)zbwphotosmanager{
    static ZBWPhotoManager *g_zbmanager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        g_zbmanager =  [[ZBWPhotoManager alloc] init];
    });
    return g_zbmanager;
}


/// 显示相册
/// @param viewController 跳转的控制器
/// @param maxCount 最大选择数量
/// @param albumArray 返回的图片数组
+ (void)showPhotosManager:(UIViewController*)viewController withMaxImageCount:(NSInteger)maxCount withAlbumArray:(void(^)(NSMutableArray <ZBWPhotoModel*>* albumArray))albumArray {
    
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (status == PHAuthorizationStatusAuthorized) {
                
                ZBWAlbumViewController *albumViewController = [[ZBWAlbumViewController alloc]init];
                albumViewController.confirmAction = ^{
                    albumArray([ZBWPhotoManager standardPhotoManger].photoModelList);
                };
                [ZBWPhotoManager standardPhotoManger].maxCount = maxCount;
                
                UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:albumViewController];
                nav.modalPresentationStyle = UIModalPresentationFullScreen;
                [viewController presentViewController:nav animated:YES completion:nil];
                
            }else {
                
                NSString *TitStr = @"提示";
                NSString *CnacelStr = @"取消";
                NSString *ToSetStr = @"去设置";
                NSString *message = @"请在设置中，允许“One”访问您的照片";
                UIAlertController * alertController = [UIAlertController alertControllerWithTitle:TitStr message:message preferredStyle:UIAlertControllerStyleAlert];
                
                [alertController addAction:[UIAlertAction actionWithTitle:CnacelStr style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                }]];
                [alertController addAction:[UIAlertAction actionWithTitle:ToSetStr style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                    NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                    if ([[UIApplication sharedApplication]canOpenURL:url]) {
//                        [[UIApplication sharedApplication]openURL:url];
                        [[UIApplication sharedApplication]openURL:url options:nil completionHandler:nil];
                    }
                }]];
                [viewController presentViewController:alertController animated:YES completion:nil];
            }
        });
    }];
}
@end
